<template>
    <el-container>
        <el-container class="is-vertical">
            <div class="panel-search-box">
                <div class="panel-search">
                    <el-input v-model="search.name" placeholder="教练姓名" clearable></el-input>
                    <el-input v-model="search.mobile" placeholder="手机号码" clearable></el-input>
                    <el-input v-model="search.agentName" placeholder="机构" clearable></el-input>
                    <el-select v-model="search.level" placeholder="等级" clearable>
                        <el-option label="初级" :value="1"></el-option>
                        <el-option label="中级" :value="2"></el-option>
                        <el-option label="高级" :value="3"></el-option>
                    </el-select>
                    <el-select v-model="search.type" placeholder="申请类型" clearable>
                        <el-option label="新增" :value="1"></el-option>
                        <el-option label="资料修改" :value="2"></el-option>
                    </el-select>
                    <el-select v-model="search.status" placeholder="审核状态" clearable>
                        <el-option label="待审核" :value="0"></el-option>
                        <el-option label="审核通过" :value="1"></el-option>
                        <el-option label="审核拒绝" :value="2"></el-option>
                    </el-select>
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                </div>
            </div>
            <el-main class="nopadding">
                <scTable ref="table" :apiObj="$API.teacher.audit.list" remoteSort :params="search" @selection-change="selectionChange" @dataChange="dataChange">
                    <el-table-column label="序号" prop="index" width="70" align="left"></el-table-column>
                    <el-table-column label="姓名" prop="name" align="center"></el-table-column>
                    <el-table-column label="申请类型" prop="type" align="center" width="100">
                        <template #default="scope">
                            {{ scope.row.type ? scope.row.type.name : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="等级" prop="level" width="120" align="center">
                        <template #default="scope">
                            {{ scope.row.level ? scope.row.level.name : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="头衔" prop="title" width="150" align="center">
                        <template #default="scope">
                            {{ scope.row.title ? scope.row.title.name : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="所属机构" prop="agent" width="150" align="center">
                        <template #default="scope">
                            {{ scope.row.agent ? scope.row.agent.name : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="手机号" prop="mobile" width="120" align="center">
                    </el-table-column>
                    <el-table-column label="年龄" prop="age" width="80" align="center">
                    </el-table-column>
                    <el-table-column label="状态" prop="status" width="120" align="center">
                        <template #default="scope">
                            <el-tag size="mini" v-if="scope.row.status.value == 0" type="warning">{{scope.row.status.name}}</el-tag>
                            <el-tag size="mini" v-if="scope.row.status.value == 1" type="success">{{scope.row.status.name}}</el-tag>
                            <el-tag size="mini" v-if="scope.row.status.value == 2" type="danger">{{scope.row.status.name}}
                            </el-tag>
                            <template v-if="scope.row.status.remark">
                                <el-tooltip class="box-item" effect="dark" :content="scope.row.status.remark" placement="top-end">
                                    <i style="cursor: pointer" class="el-icon-question table-msg" />
                                </el-tooltip>
                            </template>
                        </template>
                    </el-table-column>
                    <el-table-column label="申请时间" prop="createdAt" width="150" align="center"></el-table-column>
                    <el-table-column label="操作" fixed="right" align="center" width="150">
                        <template #default="scope">
                            <el-button type="text" size="small" @click="table_show(scope.row, scope.$index)">查看</el-button>
                            <el-divider direction="vertical" v-if="permission.audit && scope.row.status.value == 0"></el-divider>
						    <el-button type="text" size="small" v-if="permission.audit && scope.row.status.value == 0" @click="table_audit(scope.row)">审核</el-button>
                            <el-divider v-if="permission.delete" direction="vertical"></el-divider>
                            <el-popconfirm v-if="permission.delete" :title="`确认是否删除: ${scope.row.name}?`" @confirm="table_del(scope.row, scope.$index)">
                                <template #reference>
                                    <el-button type="text" size="small">删除</el-button>
                                </template>
                            </el-popconfirm>
                        </template>
                    </el-table-column>
                </scTable>
            </el-main>
        </el-container>
    </el-container>

    <detail v-if="dialog.detail" ref="detailDialog"></detail>

    <auditDetail v-if="dialog.auditDetail" ref="auditDetailDialog"></auditDetail>

    <auditDialog v-if="dialog.audit" ref="auditDialog" @success="handleSuccess" @closed="dialog.audit=false"></auditDialog>

</template>

<script>
import { rolePermission } from '@/utils/permission'
import detail from './detail'
import auditDetail from './auditDetail'
import auditDialog from './components/audit'
export default {
    name: 'teacher',
    components: {
        detail,
        auditDetail,
        auditDialog
    },
    data() {
        return {
            dialog: {
                detail: false,
                auditDetail: false,
                audit: false
            },
            selection: [],
            search: {
                name: null,
            },
            permission: {
				audit: rolePermission('teacher-audit-status'),
                delete: rolePermission('teacher-audit-delete'),
			},
            teacherTitles: []
        }
    },
    mounted() {
        this.getAllTeacherTitle()
    },
    methods: {
        getAllTeacherTitle() {
            this.$API.teacher.title.all.get({}).then(res => {
                this.teacherTitles = res.data
            })
        },
        dataChange(res, tableData) {
			let total = res.data.total
            let perPage = res.data.perPage
            let page = (this.$refs.table && this.$refs.table.currentPage) || 1
            tableData.forEach((item, index) => {
				item.index = total - (page * perPage + index - perPage)
            })
        },
        // 搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        //查看
        async table_show(row) {
            if (row.type.value == 2) {
                this.dialog.auditDetail = true
            }else{
                this.dialog.detail = true
            }
            this.$nextTick(() => {
                if (row.type.value == 2) {
                    this.$refs.auditDetailDialog.open(row.id, 'first')
                }else{
                    this.$refs.detailDialog.open(row.id, 'first')
                }
            })
        },
        //审核
        table_audit(row) {
            this.dialog.audit = true
            this.$nextTick(() => {
				let title = '教练申请审核'
				if(row.type.value == 2) {
					title = '教练信息修改审核'
				}
                this.$refs.auditDialog.open(title).setData(row)
            })
        },
        //删除
        async table_del(row) {
            await this.$API.teacher.audit.action.delete({ id: row.id }).then(() => {
                this.$notify.success("删除成功")
                this.$refs.table.refresh()
            })
        },
        //表格选择后回调事件
        selectionChange(selection) {
            this.selection = selection;
        },
        //本地更新数据
        handleSuccess() {
            this.$refs.table.refresh()
        }
    }
}
</script>

<style>
</style>
